package com.spark.modules.online.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.core.Result;
import com.spark.common.core.entity.online.OnlineDataSource;
import com.spark.common.query.QueryGenerator;
import com.spark.common.utils.DataSourceUtils;
import com.spark.modules.online.service.IOnlineDataSourceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 系统数据字典项Controller
 *
 * @author LYCHEE
 * @date 2025/02/21 12:04
 */
@RestController
@RequestMapping(value = "/online/dataSource")
public class OnlineDataSourceController {

    @Resource
    private IOnlineDataSourceService onlineDataSourceService;

    /**
     * 分页列表查询
     *
     * @param onlineDataSource 系统数据字典项对象
     * @param pageNo           分页
     * @param pageSize         分页
     * @return 系统数据字典项列表
     */
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(OnlineDataSource onlineDataSource,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<OnlineDataSource> queryWrapper = QueryGenerator.initQueryWrapper(onlineDataSource);
        Page<OnlineDataSource> page = new Page<OnlineDataSource>(pageNo, pageSize);
        IPage<OnlineDataSource> pageList = onlineDataSourceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param onlineDataSource 系统数据字典项对象
     * @return 系统数据字典项列表
     */
    @GetMapping(value = "/queryList")
    public Result<?> queryList(OnlineDataSource onlineDataSource) {
        QueryWrapper<OnlineDataSource> queryWrapper = QueryGenerator.initQueryWrapper(onlineDataSource);
        List<OnlineDataSource> list = onlineDataSourceService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 系统数据字典项对象
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        OnlineDataSource onlineDataSource = onlineDataSourceService.getById(id);
        if (Objects.isNull(onlineDataSource)) {
            return Result.fail("数据获取失败");
        }
        return Result.ok(onlineDataSource);
    }

    /**
     * 新增
     *
     * @param onlineDataSource 系统数据字典项对象
     * @return 成功或失败
     */
    @PostMapping(value = "/add")
    public Result<?> add(@Validated @RequestBody OnlineDataSource onlineDataSource) {
        boolean save = onlineDataSourceService.save(onlineDataSource);
        return Result.flag(save);
    }

    /**
     * 编辑
     *
     * @param onlineDataSource 系统数据字典项对象
     * @return 成功或失败
     */
    @PostMapping(value = "/edit")
    public Result<?> edit(@Validated @RequestBody OnlineDataSource onlineDataSource) {
        boolean update = onlineDataSourceService.updateById(onlineDataSource);
        return Result.flag(update);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) {
        boolean update = onlineDataSourceService.removeById(id);
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = onlineDataSourceService.removeBatchByIds(Arrays.asList(ids));
        return Result.flag(update);
    }

    /**
     * 测试连接数据源
     *
     * @param id 主键
     * @return 连接是否成功
     */
    @GetMapping(value = "/testConnect")
    public Result<?> testConnect(@RequestParam String id) {
        OnlineDataSource dataSource = onlineDataSourceService.getById(id);
        if (Objects.isNull(dataSource)) {
            return Result.fail("数据获取失败");
        }
        try {
            new DataSourceUtils(dataSource.getDbDriver(), dataSource.getDbUrl()
                    , dataSource.getDbUsername(), dataSource.getDbPassword()).testConnect();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok("连接成功");
    }
}
